package com.dudu.access;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dudu.utils.ProperyResource;
import com.huawei.mediax.webservice.MediaX_WebServiceStub;
import com.huawei.mediax.webservice.types.Account;
import com.huawei.mediax.webservice.types.ChargeMode;
import com.huawei.mediax.webservice.types.ConferenceInfo;
import com.huawei.mediax.webservice.types.ConferenceKey;
import com.huawei.mediax.webservice.types.ConferenceMode;
import com.huawei.mediax.webservice.types.CreateScheduleConference;
import com.huawei.mediax.webservice.types.CreateScheduleConferenceDocument;
import com.huawei.mediax.webservice.types.CreateScheduleConferenceResponseDocument;
import com.huawei.mediax.webservice.types.Filter;
import com.huawei.mediax.webservice.types.InviteAllSubscriber;
import com.huawei.mediax.webservice.types.InviteAllSubscriberDocument;
import com.huawei.mediax.webservice.types.InviteAllSubscriberResponseDocument;
import com.huawei.mediax.webservice.types.ListConference;
import com.huawei.mediax.webservice.types.ListConferenceDocument;
import com.huawei.mediax.webservice.types.ListConferenceResponseDocument;
import com.huawei.mediax.webservice.types.Login;
import com.huawei.mediax.webservice.types.LoginDocument;
import com.huawei.mediax.webservice.types.LoginResponseDocument;
import com.huawei.mediax.webservice.types.LoginResult;
import com.huawei.mediax.webservice.types.LoginType;
import com.huawei.mediax.webservice.types.Logout;
import com.huawei.mediax.webservice.types.LogoutDocument;
import com.huawei.mediax.webservice.types.LogoutResponseDocument;
import com.huawei.mediax.webservice.types.MediaType;
import com.huawei.mediax.webservice.types.ProlongConference;
import com.huawei.mediax.webservice.types.ProlongConferenceDocument;
import com.huawei.mediax.webservice.types.ProlongConferenceResponseDocument;
import com.huawei.mediax.webservice.types.RemoveConference;
import com.huawei.mediax.webservice.types.RemoveConferenceDocument;
import com.huawei.mediax.webservice.types.RemoveConferenceResponseDocument;
import com.huawei.mediax.webservice.types.RemoveSubscriber;
import com.huawei.mediax.webservice.types.RemoveSubscriberDocument;
import com.huawei.mediax.webservice.types.RemoveSubscriberResponseDocument;
import com.huawei.mediax.webservice.types.SetSilence;
import com.huawei.mediax.webservice.types.SetSilenceDocument;
import com.huawei.mediax.webservice.types.SetSilenceResponseDocument;
import com.huawei.mediax.webservice.types.SubscribeConference;
import com.huawei.mediax.webservice.types.SubscribeConferenceDocument;
import com.huawei.mediax.webservice.types.SubscribeConferenceResponseDocument;
import com.huawei.mediax.webservice.types.Subscriber;
import com.huawei.mediax.webservice.types.VersionDocument;

import core.CoreService;

/**
 * IMS 专业会议Util
 * 
 * @author
 * 
 */
public class IMSTXUtils {

    public IMSTXUtils() {
    }

    /**
     * 登录会议
     * 
     * @param soapUserName
     *            用户名
     * @param soapPassword
     *            密码
     * @return
     * @throws Exception
     */
    public static LoginResult login(String soapUserName, String soapPassword) throws Exception {
	String username = soapUserName;
	String password = soapPassword;

	Account account = com.huawei.mediax.webservice.types.Account.Factory.newInstance();
	account.setName(username);
	account.setType(LoginType.WEB);
	account.setPassword(password);

	Login login = com.huawei.mediax.webservice.types.Login.Factory.newInstance();
	login.setAccount(account);

	LoginDocument loginDocument = com.huawei.mediax.webservice.types.LoginDocument.Factory.newInstance();
	loginDocument.setLogin(login);

	VersionDocument versionDocument = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	versionDocument.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	LoginResponseDocument loginResponseDocument = stub.login(loginDocument, versionDocument);

	LoginResult loginResult = loginResponseDocument.getLoginResponse();
	return loginResult;
    }

    /**
     * 二次登录会议
     * 
     * @param sessionId
     *            登录返回ID
     * @param conferenceId
     *            会议标识
     * @param chirepass
     *            会议密码
     * @return
     * @throws Exception
     */
    public static LoginResult secondLogin(String sessionId, String conferenceId, String chirepass) throws Exception {
	Account account = com.huawei.mediax.webservice.types.Account.Factory.newInstance();
	account.setType(LoginType.CONFERENCE_ID);
	account.setName(conferenceId);
	account.setPassword(chirepass);

	Login login = com.huawei.mediax.webservice.types.Login.Factory.newInstance();
	login.setAccount(account);
	login.setSession(sessionId);

	LoginDocument loginDocument = com.huawei.mediax.webservice.types.LoginDocument.Factory.newInstance();
	loginDocument.setLogin(login);

	VersionDocument versionDocument = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	versionDocument.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	LoginResponseDocument loginResponseDocument = stub.login(loginDocument, versionDocument);

	LoginResult seloginResult = loginResponseDocument.getLoginResponse();
	return seloginResult;
    }

    /**
     * 创建会议
     * 
     * @param sessionId
     *            登录返回ID
     * @param length
     *            IMS专业会议发起会议持续最大时间
     * @param subject
     *            会议主题
     * @param maxCount
     *            设置最大参会人数
     * @return
     * @throws Exception
     */
    public static CreateScheduleConferenceResponseDocument createSheduleConference(String sessionId, long length, String subject, int maxCount) throws Exception {
	ConferenceInfo conferenceInfo = com.huawei.mediax.webservice.types.ConferenceInfo.Factory.newInstance();
	conferenceInfo.setTimeZone(56);
	conferenceInfo.setLength(length);
	conferenceInfo.setSize(maxCount);
	conferenceInfo.setSubject(subject);

	MediaType mediaType = com.huawei.mediax.webservice.types.MediaType.Factory.newInstance();
	com.huawei.mediax.webservice.types.MediaType.Enum mediaTypes[] = new com.huawei.mediax.webservice.types.MediaType.Enum[1];
	mediaTypes[0] = MediaType.VOICE;
	conferenceInfo.setMediaTypesArray(mediaTypes);

	conferenceInfo.setIsAllowInvite(true);
	conferenceInfo.setChargeMode(ChargeMode.SCHEDULER);
	conferenceInfo.setLanguage("Chinese");
	conferenceInfo.setConferenceMode(ConferenceMode.COMMON);

	CreateScheduleConference createconference = com.huawei.mediax.webservice.types.CreateScheduleConference.Factory.newInstance();
	createconference.setSession(sessionId);
	createconference.setScheduleConferneceInfo(conferenceInfo);

	CreateScheduleConferenceDocument createScheduleConferenceDocument = com.huawei.mediax.webservice.types.CreateScheduleConferenceDocument.Factory.newInstance();
	createScheduleConferenceDocument.setCreateScheduleConference(createconference);

	VersionDocument versionDocument = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	versionDocument.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	CreateScheduleConferenceResponseDocument responseDoc = stub.createScheduleConference(createScheduleConferenceDocument, versionDocument);

	if (responseDoc.getCreateScheduleConferenceResponse().getCode() != 200 && responseDoc.getCreateScheduleConferenceResponse().getCode() != 202)
	    throw new Exception((new StringBuilder("\u9519\u8BEF\u7801=")).append(responseDoc.getCreateScheduleConferenceResponse().getCode()).toString());
	else
	    return responseDoc;
    }

    /**
     * 移除会议
     * 
     * @param session
     *            登录返回ID
     * @param conferenceID
     *            会议标识
     * @return
     * @throws Exception
     */
    public static RemoveConferenceResponseDocument removeConference(String session, String conferenceID) throws Exception {
	RemoveConference removeConference = com.huawei.mediax.webservice.types.RemoveConference.Factory.newInstance();
	removeConference.setSession(session);

	ConferenceKey conferencekey = com.huawei.mediax.webservice.types.ConferenceKey.Factory.newInstance();
	conferencekey.setConferenceID(conferenceID);
	removeConference.setConferenceKey(conferencekey);

	RemoveConferenceDocument rcd = com.huawei.mediax.webservice.types.RemoveConferenceDocument.Factory.newInstance();
	rcd.setRemoveConference(removeConference);

	VersionDocument vd = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	vd.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	RemoveConferenceResponseDocument rcrd = stub.removeConference(rcd, vd);
	return rcrd;
    }

    /**
     * 邀请与会人员
     * 
     * @param session
     *            登录返回ID
     * @param conferenceId
     *            会议标识
     * @param callees
     *            邀请人电话
     * @return
     * @throws RemoteException
     */
    public static InviteAllSubscriberResponseDocument InviteAllSubscriber(String session, String conferenceId, String callees) throws RemoteException {
	CoreService.log.info((new StringBuilder("inviteAllSubscribe session =")).append(session).toString());
	ConferenceKey conferenceKey = com.huawei.mediax.webservice.types.ConferenceKey.Factory.newInstance();
	conferenceKey.setConferenceID(conferenceId);

	List lists = new ArrayList();
	IsMobileNOUtils isMobileNOUtils = new IsMobileNOUtils();
	String callee[] = callees.split(",");
	for (int i = 0; i < callee.length; i++) {
	    Subscriber subscriber1 = com.huawei.mediax.webservice.types.Subscriber.Factory.newInstance();
	    subscriber1.setConferenceKey(conferenceKey);
	    CoreService.log.info("IMSTXUtils subscriber1_id=" + subscriber1.getSubscriberID());

	    String calleeStr = isMobileNOUtils.disposePhoneNum(callee[i], "0");
	    subscriber1.setSubscriberID(calleeStr);

	    lists.add(subscriber1);
	}

	Subscriber subscribers[] = new Subscriber[lists.size()];
	for (int i = 0; i < subscribers.length; i++)
	    subscribers[i] = (Subscriber) lists.get(i);

	InviteAllSubscriber allSubcriber = com.huawei.mediax.webservice.types.InviteAllSubscriber.Factory.newInstance();
	allSubcriber.setSession(session);
	allSubcriber.setSubscribersArray(subscribers);

	InviteAllSubscriberDocument inviteDoc = com.huawei.mediax.webservice.types.InviteAllSubscriberDocument.Factory.newInstance();
	inviteDoc.setInviteAllSubscriber(allSubcriber);

	VersionDocument versionDocument = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	versionDocument.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	InviteAllSubscriberResponseDocument inviteAllSubscriberResponseDocument = stub.inviteAllSubscriber(inviteDoc, versionDocument);
	return inviteAllSubscriberResponseDocument;
    }

    /**
     * 移除参会者
     * 
     * @param session
     *            登录返回ID
     * @param conferenceId
     *            会议标识
     * @param callId
     *            移除人电话
     * @return
     * @throws RemoteException
     */
    public static RemoveSubscriberResponseDocument removeSubscriber(String session, String conferenceId, String callId) throws RemoteException {
	RemoveSubscriber rmSubscriber = com.huawei.mediax.webservice.types.RemoveSubscriber.Factory.newInstance();
	rmSubscriber.setConferenceID(conferenceId);
	rmSubscriber.setSession(session);
	if (callId != null) {
	    rmSubscriber.setCallID(callId);
	}
	CoreService.log.info("被删除的参会人ID为：postCallId=" + callId);

	RemoveSubscriberDocument rmSubscriberDoc = com.huawei.mediax.webservice.types.RemoveSubscriberDocument.Factory.newInstance();
	rmSubscriberDoc.setRemoveSubscriber(rmSubscriber);

	VersionDocument vd = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	vd.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	RemoveSubscriberResponseDocument rsrd = stub.removeSubscriber(rmSubscriberDoc, vd);
	return rsrd;
    }

    /**
     * 订购会议
     * 
     * @param session
     *            登录返回ID
     * @param conferenceId
     *            会议标识
     * @return
     * @throws Exception
     */
    public static SubscribeConferenceResponseDocument subcribeConference(String session, String conferenceId) throws Exception {
	SubscribeConference subscribeConference = com.huawei.mediax.webservice.types.SubscribeConference.Factory.newInstance();
	subscribeConference.setAddress(ProperyResource.getSoapStatusCallbackAdd());
	subscribeConference.setSession(session);

	ConferenceKey conferenceKey = com.huawei.mediax.webservice.types.ConferenceKey.Factory.newInstance();
	conferenceKey.setConferenceID(conferenceId);
	subscribeConference.setConferenceKey(conferenceKey);

	SubscribeConferenceDocument subcribeConfDoc = com.huawei.mediax.webservice.types.SubscribeConferenceDocument.Factory.newInstance();
	subcribeConfDoc.setSubscribeConference(subscribeConference);

	VersionDocument vd = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	vd.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	SubscribeConferenceResponseDocument subConfResDoc = stub.subscribeConference(subcribeConfDoc, vd);
	return subConfResDoc;
    }

    /**
     * 列出会议
     * 
     * @param session
     *            登录返回ID
     * @return
     * @throws Exception
     */
    public static ListConferenceResponseDocument listConfenrence(String session) throws Exception {
	Filter filter = com.huawei.mediax.webservice.types.Filter.Factory.newInstance();
	filter.setPageSize(20);
	filter.setPageIndex(0);
	filter.setIsAscend(true);

	ListConference conferences = com.huawei.mediax.webservice.types.ListConference.Factory.newInstance();
	conferences.setConferenceFilter(filter);
	conferences.setSession(session);

	ListConferenceDocument listdoc = com.huawei.mediax.webservice.types.ListConferenceDocument.Factory.newInstance();
	listdoc.setListConference(conferences);

	VersionDocument vd = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	vd.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	ListConferenceResponseDocument ret = stub.listConference(listdoc, vd);
	return ret;
    }

    /**
     * 静音/取消静音
     * 
     * @param callId
     * @param conferenceId
     *            会议标识
     * @param session
     *            登录返回ID
     * @param isSilence
     *            静音/取消静音
     * @return
     * @throws RemoteException
     */
    public static SetSilenceResponseDocument setLicenseState(String callId, String conferenceId, String session, boolean isSilence) throws RemoteException {
	String postCallId = callId;

	SetSilence setSilence = com.huawei.mediax.webservice.types.SetSilence.Factory.newInstance();
	setSilence.setOperation(isSilence);
	setSilence.setSession(session);
	if (postCallId != null)
	    setSilence.setCallID(postCallId);

	ConferenceKey conferencekey = com.huawei.mediax.webservice.types.ConferenceKey.Factory.newInstance();
	conferencekey.setConferenceID(conferenceId);
	setSilence.setConferenceKey(conferencekey);

	SetSilenceDocument silenceDoc = com.huawei.mediax.webservice.types.SetSilenceDocument.Factory.newInstance();
	silenceDoc.setSetSilence(setSilence);

	VersionDocument vd = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	vd.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	SetSilenceResponseDocument silenceresPDoc = stub.setSilence(silenceDoc, vd);
	return silenceresPDoc;
    }

    /**
     * 退出登陆
     * 
     * @param sessionId
     *            登录返回ID
     * @return
     * @throws Exception
     */
    public static LogoutResponseDocument logout(String sessionId) throws Exception {
	Logout logoutbean = com.huawei.mediax.webservice.types.Logout.Factory.newInstance();
	logoutbean.setSession(sessionId);

	LogoutDocument logout = com.huawei.mediax.webservice.types.LogoutDocument.Factory.newInstance();
	logout.setLogout(logoutbean);

	VersionDocument versionDocument = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	versionDocument.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	LogoutResponseDocument loginResponseDocument = stub.logout(logout, versionDocument);
	return loginResponseDocument;
    }

    /**
     * 延长会议
     * 
     * @param sessionId
     *            登录返回ID
     * @param conferId
     *            会议标识
     * @param length
     *            时间
     * @return
     * @throws Exception
     */
    public static ProlongConferenceResponseDocument prolongConference(String sessionId, String conferId, long length) throws Exception {
	ProlongConference prolongConference = ProlongConference.Factory.newInstance();
	prolongConference.setSession(sessionId);
	prolongConference.setConferenceID(conferId);
	prolongConference.setLength(length);

	ProlongConferenceDocument prolongConferenceDocument = ProlongConferenceDocument.Factory.newInstance();
	prolongConferenceDocument.setProlongConference(prolongConference);

	VersionDocument vd = com.huawei.mediax.webservice.types.VersionDocument.Factory.newInstance();
	vd.setVersion(version);

	MediaX_WebServiceStub stub = new MediaX_WebServiceStub(ProperyResource.getSOAP_ABILITY());
	ProlongConferenceResponseDocument prolongConferenceResponseDocument = stub.prolongConference(prolongConferenceDocument, vd);
	CoreService.log.info("prolongConferenceResponseDocument=" + prolongConferenceResponseDocument.toString());

	return prolongConferenceResponseDocument;

    }

    private static String version = "V300R008C02";
    // private static String SOAP_ABILITY_test =
    // "http://202.109.211.101:8666/axis/services/MediaXWebServicePort";
    // private static String SOAP_ABILITY =
    // "http://61.131.50.38:8666/axis/services/MediaXWebServicePort";
    public static Map mCallIdMap = new HashMap();

    public static void main(String[] args) {

	try {
	    prolongConference("55402214829359468695000-0010", "81321800", 300000);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}
